// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var leftMenu = require("../leftMenu/leftMenu");
	var topMenu = require("../topMenu/topMenu");
	var router = require("../hash/pageRouter");
	var footer = require("../footer/footer_main");
	var firstVisited = 0;
	var dialog = require("dialog");
	var validate = require("validate");
	var newsCategoryIdDic = {
    	enabled:0,
    	newsCategoryId:0
    };
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var imgSrc;
	var init = function(num) {
		firstVisited = base.getUrlParam("firstVisited");
		leftMenu.init(num);
		topMenu.init(4);
		$("#project").removeClass("topMenu_selected");
		$("#news").addClass("topMenu_selected");
		$(".footer").removeClass("none");
		initEvent();
		router.init();
		inquireNewsPageList();
		inquireNewsCategoryPageList();
		
		if(firstVisited != 1){
			$(".table-section").removeClass("none");
		}
	};
	var initEvent = function() {
		footer.init(); 
		$("#footer").removeClass("none");
		//若未登录，则返回登陆页面
		if(base.getData("sid")==null||base.getData("sid")==""||base.getData("sid")==undefined){
			top.location.href="../login/login.html";
		}
		
		//回车键绑定
		document.onkeydown = function(e) {
			if (!e) e = window.event; //火狐中是 window.event
			if ((e.keyCode || e.which) == 13) {
				pageIndex = 1;
				inquireNewsPageList();
			}
		};

		/*新增的点击事件*/

		$("#news-add").on("click", function() {
			router.route("news/news_add.html");
		});

		$("#leftMenu").on("click", "#news", function() {
			location.href="../news/news.html";
		});

		$("#leftMenu").on("click", "#news-category", function() {
			router.route("newsCategory/news_category_list.html");
		});

		//搜索按钮
		$("#newsListButton").on("click", function() {
			pageIndex = 1;
			pageSize = 10;
			totalPageCount = 1;
			totalRecordCount = 0; //分页初始化
			inquireNewsPageList();
		});
	};
	
	//查询新闻资讯列表
	var inquireNewsPageList = function() {
		var issueDateFromTime =$("#issueDateFromTime").val();
		var issueDateToTime =$("#issueDateToTime").val();
		var newsCategoryId = $("#newsCategoryId").val();
		var publisher = $("#publisher").val();
		var newsTitle = $("#newsTitle").val();
		var recommend = $("#recommend").val();
		base.fnAjax({
			action: "inquireAllNewsPageList.json",
			async: false, //同步异步
			nocode: false, //是否要登陆，true为不用登陆
			oData: {
				newsCategoryId : newsCategoryId,
				publisher : publisher,
				newsTitle :newsTitle,
				recommend : recommend,
				pageIndex : pageIndex,
				pageSize : pageSize,
				issueDateFromTime:issueDateFromTime,
				issueDateToTime:issueDateToTime
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#newsListTpl").html();
					var html = juicer(tpl, data);
					$("#newsListBody").html(html);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl($("#newspagenation"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireNewsPageList();
						});
					$(".newsListDetail").unbind("click").click(function(){
						router.route("news/news_add.html?newsId="+$(this).parent().parent().attr("newsId"));
					});
					
					$(".table_delete_btn").unbind("click").click(function(){
						var newsId =$(this).parent().parent().attr("newsId");
						dialog.showConfirmDialog("删除资讯","确定要删除此资讯吗？",function(){
							disableNews(newsId);
						});
					});
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};
	
	//删除资讯
	var disableNews = function(newsId){
		base.fnAjax({
            action: "disableNews.json", //接口名称
            async: false, //同步异步
            oData: {
                newsId:newsId
            }, //传递参数
            fnSuccess: function(data) { //成功回调
                if (data.status == 200) {
                    inquireNewsPageList();
                } else {
                    base.alert(3, data.message);
                }
            }
        });
	}
	 // 获取资讯栏目
    var inquireNewsCategoryPageList = function(){
    	 var html = function(data) {
            var str = "";
            for (var i = 0; i < data.datas.length; i++) {
                var row = data.datas[i];
                str += '<option value="' + row.newsCategoryId + '">' + row.categoryName + '</option>';
            }
            $("#newsCategoryId").append(str);
            if (newsCategoryIdDic.enabled == 1) {
            	$("#newsCategoryId").val(newsCategoryIdDic.newsCategoryId);	
			}else{
				newsCategoryIdDic.enabled = 1;
			};
        };
        base.fnAjax({
            action: "inquireCategoryPageList.json", //接口名称
            async: false, //同步异步
            oData: {
                categoryName: "",
                pageIndex: pageIndex,
                pageSize: pageSize
            }, //传递参数
            fnSuccess: function(data) { //成功回调
                if (data.status == 200) {
                    html(data);
                } else {
                    base.alert(3, data.message);
                }
            }
        });
    };

	
	return {
		init: init
	};
});